package com.quectel.mobile.controller.partybuilding;

import cn.hutool.core.lang.func.LambdaUtil;
import com.quectel.core.module.partybuilding.dto.PartyPersonDto;
import com.quectel.core.module.partybuilding.service.PartyOrganizationService;
import com.quectel.core.module.partybuilding.service.PartyPersonService;
import com.quectel.core.module.partybuildingnotice.dto.PartyBuildingNoticeDto;
import com.quectel.mobile.controller.BaseController;
import com.quectel.mobile.util.SessionHolder;
import com.quectel.util.common.Page;
import com.quectel.util.common.Request;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 在这里编写说明
 *
 * @author: bob
 * @email: bob.yu@quectel.com
 * @date: 2023-06-13 13:58:00
 */
@RestController
@RequestMapping("partyOrganization")
@Tag(name = "PartyOrganizationController", description = "智慧党建-党组织相关api")
public class PartyOrganizationController extends BaseController {

    @DubboReference
    private PartyOrganizationService partyOrganizationService;
    @DubboReference
    private PartyPersonService partyPersonService;

    @GetMapping("myPartyOrganization")
    @Operation(summary = "我的党组织")
    public Response<Page<PartyPersonDto>> myPartyOrganization(
            @Parameter(description = "党员姓名") @RequestParam(required = false) String name,
            @Parameter(description = "从1开始的页码数") @RequestParam Integer pageNo,
            @Parameter(description = "每页多少条") @RequestParam Integer pageSize) {
        SessionHolder.checkPartyPerson();

        Request request = Request.configParams(Request.parseParams(httpServletRequest));

        request.getParams().put(LambdaUtil.getFieldName(PartyBuildingNoticeDto::getPartyId),
                SessionHolder.getLoginMobileUserSession().getPartyPerson().getPartyId());

        int total = partyPersonService.queryTotal(request.getParams());
        List<PartyPersonDto> list = null;
        if (total > 0) {
            list = partyPersonService.queryList(request.getParams());
        } else {
            list = new ArrayList<>();
        }

        Page<PartyPersonDto> page = new Page<>(request.getPageNo(), request.getPageSize(), total, list);

        return Response.<Page<PartyPersonDto>>ok().wrap(page);
    }

    @GetMapping("myInfo")
    @Operation(summary = "个人党员信息")
    public Response<PartyPersonDto> myInfo(){
        SessionHolder.checkPartyPerson();
        Long id = Objects.requireNonNull(SessionHolder.getLoginMobileUserSession()).getPartyPerson().getId();
        PartyPersonDto partyPersonDto = partyPersonService.selectById(id);
        return Response.<PartyPersonDto>ok().wrap(partyPersonDto);
    }

}
